# source:https://leetcode.cn/problems/rabbits-in-forest/?envType=daily-question&envId=2025-04-20 贪心
class Solution:
    def numRabbits(self, answers: List[int]) -> int:
        answers.sort()
        i = 0
        ans = 0
        while i < len(answers):
            if answers[i] == 0:
                ans += 1
                i += 1
            else:
                j = i
                while j < len(answers)-1 and answers[j] == answers[j+1]:
                    if j+1-i > answers[i]:
                        break
                    j += 1
                ans += (1+answers[j])
                i = j+1
        return ans

# source:https://leetcode.cn/problems/count-the-hidden-sequences/?envType=daily-question&envId=2025-04-21 
class Solution:
    def numberOfArrays(self, differences: List[int], lower: int, upper: int) -> int:
        num = 0
        mx, mn = 0, 0
        for v in differences:
            num += v
            mx = max(mx, num)
            mn = min(mn, num)
        # print(mn, mx)
        if mn <= lower:
            tmp = (lower-mn)
            mn += tmp
            mx += tmp 
        else:
            tmp = (mn-lower)
            mn -= tmp
            mx -= tmp
        # print(mn, mx)
        if mx <= upper:
            return upper-mx+1
        return 0
        

